import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;

class calendar {
    JFrame f=new JFrame();
    JPanel p1=new JPanel();	//显示上下月操作按钮
    JPanel p2=new JPanel();	//显示日历
    JPanel p3=new JPanel();	//显示日历的年月
    JLabel l0=new JLabel("日历:",JLabel.CENTER);
    String s[]={"日","一","二","三", "四","五","六"};
    JLabel l[]=new JLabel[49]; 	//一共有7*7=49个标签

    Calendar cal=new GregorianCalendar();
    JTextField t1=new JTextField();	//接受年
    JTextField t2=new JTextField();	//接受月
    void time(boolean b,int x) {	//需要输入年月是用true，其他为false
        for(int i=7;i<49;i++) {		//恢复日期表为空白状态
            l[i].setText("");
        }
        if(b&&t1.getText()!=null&&t2.getText()!=null)
            if(Integer.valueOf(t1.getText())>=1970&&Integer.valueOf(t1.getText())<=2050&&Integer.valueOf(t2.getText())>=1&&Integer.valueOf(t2.getText())<=12)
            //限定1970-2050年的1-12月
            {cal.set(Calendar.YEAR,Integer.valueOf(t1.getText()));cal.set(Calendar.MONTH,Integer.valueOf(t2.getText())-1);}		//根据文本框设置年月
        if(x!=0) cal.set(Calendar.MONTH,(cal.get(Calendar.MONTH)+x)); //根据标量设置月份
        l0.setText("日历:"+cal.get(Calendar.YEAR)+"年"+(cal.get(Calendar.MONTH)+1)+"月");
        int date=1;					//设置日期数从一号开始
        cal.set(Calendar.DATE,1);	//将日期设为一号
        for(int i=6+cal.get(Calendar.DAY_OF_WEEK);date<=cal.getActualMaximum(Calendar.DATE);date++,i++)
        //将日期设为一号得到一号为星期几，从一号开始一直设置标签直到日期数到达该月日期的最大值
        {
            l[i].setText(String.valueOf(date));
        }
    }
    public calendar() {
        f.setSize(420, 300);
        JButton last=new JButton("上月");
        last.setSize(30, 10);
        last.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                time(false,-1);
            }
        });
        JButton next=new JButton("下月");
        next.setSize(30, 10);
        next.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                time(false,1);
            }
        });
        //JLabel set=new JLabel("请输入年月:");
        p1.setLayout(new GridLayout(1,5));
        p1.add(new JLabel("请输入年月"));
        JButton sure=new JButton("确定");
        sure.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                time(true,0);
            }
        });
        t1.setSize(30, 10);
        t2.setSize(30, 10);
        p1.add(t1);
        p1.add(t2);
        p1.add(sure);
        p1.add(last);
        p1.add(next);
        p2.setLayout(new GridLayout(7,7)); 	//采用7*7布局
        for(int i=0;i<7;i++)     //前面七个标签为星期几
        {
            l[i]=new JLabel(s[i],JLabel.CENTER);
            l[i].setBorder(new BevelBorder(BevelBorder.RAISED));	//标签设置为凸起
            p2.add(l[i]);
        }
        for(int i=7;i<49;i++)     //后面42个标签用来显示日期
        {
            l[i]=new JLabel("",JLabel.CENTER);
            l[i].setBorder(new BevelBorder(BevelBorder.LOWERED));	//标签设置为凹陷
            p2.add(l[i]);
        }
        time(false,0);
        p3.add(l0);
        JPanel p=new JPanel();
        f.add(p1,BorderLayout.NORTH);
        f.add(p2,BorderLayout.CENTER);
        f.add(p3,BorderLayout.SOUTH);
        f.setLocationRelativeTo(null);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
}
public class Show {
    public static void main(String []args) {
        calendar c=new calendar();
    }
}
